﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class Snake : moveObject
    {
        Animation snakeAnim;
        PlayerAnimation snakePlayAnim;

        public override Rectangle colRec
        {
            get
            {
                //return base.colRec;
                return new Rectangle((int)pos.X,(int)pos.Y, snakeAnim.FrameWidth, snakeAnim.FrameHeight);
            }
        }

        bool hitWall = false;

        public Snake(Direction facing,float time, float x, float y,float velx, int pace)
            :base(facing,time,x,y,velx,pace)
        {
            if (facing == Direction.Left)
                pos = new Vector2(x, y); // comment!!
            else
                pos = new Vector2(x - 120, y);
            timeWait = time;
            snakePlayAnim = new PlayerAnimation();
            isWaiting = false;
        }

        public override void loadContent(Level lvl)
        {
            snakeAnim = new Animation(lvl.Content.Load<Texture2D>("images/snakeanim"), 0.1f, true, 120,0);
            //tex = lvl.Content.Load<Texture2D>("images/snake");
            snakePlayAnim.PlayAnimation(snakeAnim);
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Rectangle viewPort)
        {
            //base.Update(gameTime, viewPort);

            if (!isWaiting) // if the truck isn't waiting
            {
                // then the position on the x axis is increased (or decreased) depending on the enum stored
                switch (dir)
                {
                    case Direction.Left:
                        pos -= vel;
                        break;
                    case Direction.Right:
                        pos += vel;
                        break;
                    default:
                        break;
                }


                if (pos.X < 0 && !hitWall) // if the truck is no longer on screen
                {
                    // it is set to wait

                    if (!hitWall)
                    {
                        dir = Direction.Right;
                        hitWall = true;
                    }
                }
                else if (!viewPort.Intersects(colRec) && hitWall) // if the truck is no longer on screen
                {
                        // its position on the x axis is reset to its starting location
                        //switch (dir)
                        //{
                        //    case Direction.Left:
                        //        pos.X = viewPort.Right;
                        //        break;
                        //    case Direction.Right:
                        //        pos.X = viewPort.Left - tex.Width;
                        //        break;
                        //    default:
                        //        break;
                        //}
                    isWaiting = true;
                    
                }
            }
            else // it's waiting for the time given to wait is over
            {
                // the variable is assigned the elapsed game time from the the gameTime object
                // that is passed as a parameter..
                timeTaken += gameTime.ElapsedGameTime.Milliseconds;

                // ...and is checked to see if is equal or greater than the max amount of time to wait
                if (timeTaken >= timeWait)
                {
                    isWaiting = false; // if it is equal or greater, then its set to not wait (moving)
                    dir = Direction.Left;
                    hitWall = false;

                    timeTaken -= timeWait;
                    //timeTaken = 0;
                }
            }

        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //base.Draw(spriteBatch, gameTime);
            switch (dir)
            {
                case Direction.Left:
                    snakePlayAnim.Draw(gameTime, spriteBatch, pos, SpriteEffects.None, Color.White);
                    break;
                case Direction.Right:
                    snakePlayAnim.Draw(gameTime, spriteBatch, pos, SpriteEffects.FlipHorizontally, Color.White);
                    break;
                default:
                    break;
            }
            
            //spriteBatch.Draw(tex, pos, Color.White);
        }
    } 
}
